package com.bidevalution.controller.task;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.hsqldb.jdbcDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.bidevalution.business.service.BeEvalutionExpertService;
import com.bidevalution.business.service.BeEvalutionOutcomeService;
import com.bidevalution.business.service.BeExpertVoteResultService;
import com.bidevalution.business.service.BeLayoutConfigService;
import com.bidevalution.business.service.BeMethodColumnService;
import com.bidevalution.business.service.BeMethodDefineService;
import com.bidevalution.business.service.BeMethodLayoutService;
import com.bidevalution.business.service.BeMethodStepService;
import com.bidevalution.business.service.BeParameterReferenceService;
import com.bidevalution.business.service.BeSectionExpertInfoService;
import com.bidevalution.business.service.BeSectionExpertProgressService;
import com.bidevalution.business.service.BeSectionInfoService;
import com.bidevalution.business.service.BeSectionLayoutService;
import com.bidevalution.business.service.BeSectionMethodService;
import com.bidevalution.business.service.BeSectionParameterService;
import com.bidevalution.business.service.BeSectionStepService;
import com.bidevalution.business.service.JdbcTemplateService;
import com.bidevalution.dao.entity.BeMethodColumn;
import com.bidevalution.dao.entity.BeMethodLayout;
import com.bidevalution.dao.entity.BeMethodStep;
import com.bidevalution.dao.entity.BeParameterReference;
import com.bidevalution.dao.entity.BeSectionExpertInfo;
import com.bidevalution.dao.entity.BeSectionExpertProgress;
import com.bidevalution.dao.entity.BeSectionInfo;
import com.bidevalution.dao.entity.BeSectionLayout;
import com.bidevalution.dao.entity.BeSectionMethod;
import com.bidevalution.dao.entity.BeSectionParameter;
import com.bidevalution.dao.entity.BeSectionStep;
import com.bidevalution.support.util.LogUtil;
import com.bidevalution.support.util.MapUtil;
import com.bidevalution.support.util.StringUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.qtp.core.util.DateUtil;
import com.qtp.core.util.IdsUtil;

/**
 * 环节数据复制到标段环节数据
 * 
 * @author daizhifeng
 * @date 2019年9月20日 下午4:05:48
 * @version V2.0
 */

@EnableScheduling
@Component
public class SectionStepDataTask {
	@Autowired
	private BeSectionExpertProgressService beSectionExpertProgressService;
	@Autowired
	private BeSectionInfoService beSectionInfoService;
	@Autowired
	private BeSectionExpertInfoService beSectionExpertInfoService;
	@Autowired
	private BeMethodStepService beMethodStepService;
	@Autowired
	private BeSectionMethodService beSectionMethodService;
	@Autowired
	private BeSectionStepService beSectionStepService;
	@Autowired
	private JdbcTemplateService jdbcTemplateService;
	@Autowired
	private BeSectionParameterService beSectionParameterService;
	@Autowired
	private BeParameterReferenceService beParameterReferenceService;
	private Logger logger = Logger.getLogger(SectionStepDataTask.class);
	public void init() {
		Date startDate = DateUtil.stringToDate(DateUtil.getCurrentDateStr("yyyy-MM-dd") + " 00:00:00",
				"yyyy-MM-dd HH:mm:ss");
		Date endDate = DateUtil.stringToDate(DateUtil.getCurrentDateStr("yyyy-MM-dd") + " 23:59:59",
				"yyyy-MM-dd HH:mm:ss");
		for (BeSectionInfo beSectionInfo : beSectionInfoService.getListByBidOpenTime(startDate, endDate)) {
			synData(beSectionInfo);
		}
	}
	public void synData(BeSectionInfo beSectionInfo) {
		List<BeSectionStep> beSectionStepList = null;
		Map<String, Object> sectionMethodMap;
		Map<String, Object> paramMap = new HashMap<String, Object>();
		BeSectionMethod beSectionMethod = null;
		List<BeSectionExpertProgress> stepExpertProgressList;
		List<String> expertList;
		try {
			paramMap.put("projectId", beSectionInfo.getTdProjectid());
			beSectionMethod = beSectionMethodService.selectBySectionId(beSectionInfo.getSeqId());

			if (beSectionMethod == null) {
				logger.info(beSectionInfo.getSectionName()+">>>>>>>>>>>>>>>>"+beSectionInfo.getTdProjectid());
				sectionMethodMap = jdbcTemplateService.selectForMap(
						"select aume_ScoreMethods as ScoreMethods,aume_IsBidHide as IsBidHide,b.proj_Area as projArea from td_auditmethods a,td_project b where a.aume_ProjectID=:projectId and a.aume_ProjectID=b.proj_ProjectID",
						paramMap);
				
				if (sectionMethodMap != null) {
					if(StringUtils.isEmpty(sectionMethodMap.get("ScoreMethods")) || StringUtils.isEmpty(sectionMethodMap.get("projArea"))) {
						logger.error(sectionMethodMap+",ScoreMethods或者projArea为空");
						return;
					}
					beSectionMethod = beSectionMethodService.save(beSectionInfo,
							sectionMethodMap.get("ScoreMethods").toString(),
							StringUtil.toString(sectionMethodMap.get("IsBidHide")),
							sectionMethodMap.get("projArea").toString());
					if(beSectionMethod == null) {
						logger.error("方法导入失败:"+sectionMethodMap.get("ScoreMethods")+",该方法代码,区域代码："+sectionMethodMap.get("projArea"));
						return;
					}
					/**********************************赋值参数*******************/
					int cnt = beSectionParameterService.selectCountBySectionId(beSectionInfo.getSeqId(), "0");
					logger.info("导入参数个数:"+cnt+":"+beSectionInfo.getSectionName());
					if (cnt == 0) {
						Map<String, String> paramIdMap = Maps.newHashMap();
						//获取招标评标参数数据
						for (Map<String, Object> dataMap : jdbcTemplateService.selectForList(
								"select aupa_ParaName as paraName,aupa_ParaValue as paraValue from td_auditpara where aupa_ProjectID=:projectId",
								MapUtil.create().putValue("projectId", beSectionInfo.getTdProjectid()).getMap())) {
							if (dataMap.get("paraName") == null) {
								continue;
							}
							paramIdMap.put(dataMap.get("paraName").toString().toLowerCase(),
									StringUtil.toString(dataMap.get("paraValue")));
						}
						logger.info(paramIdMap);
						for (BeParameterReference beParameterReference : beParameterReferenceService.selectList(beSectionMethod.getTemplateConfig())) {
							beSectionParameterService.save(beSectionInfo.getSeqId(), paramIdMap, beParameterReference);
						}
					}
					logger.info(beSectionInfo.getSectionName()+"开始同步数据>>>>>>>>>>>>>>>>");
					//复制环节相关的配置数据
					copyDataToSection(beSectionMethod);
					logger.info(beSectionInfo.getSectionName()+"结束同步数据>>>>>>>>>>>>>>>>");
					
				}else {
					logger.info(beSectionInfo.getTdProjectid()+",在表aume_ScoreMethods不存在");
					return;
				}
				
			}
		
			/******************** 复制专家数据 **************************/
			beSectionStepList = beSectionStepService.getAllStepList(beSectionInfo.getSeqId());
			//获取所有非回避专家
			List<BeSectionExpertInfo> sectionExpertInfoList = beSectionExpertInfoService.getListBySectionId(beSectionInfo.getSeqId(),"0");
			expertList = Lists.newArrayList();
			for(BeSectionExpertInfo beSectionExpertInfo:sectionExpertInfoList) {
				expertList.add(beSectionExpertInfo.getSeqId());
			}
			label:
			for(BeSectionStep beSectionStep:beSectionStepList) {
				stepExpertProgressList = beSectionExpertProgressService.selectList(beSectionStep.getSeqId());
				//环节的专家数量不等于专家表中的数量，则重新添加专家
				if(stepExpertProgressList.size() == 0 || sectionExpertInfoList.size() != stepExpertProgressList.size()) {
					beSectionExpertProgressService.addProgressList(beSectionInfo.getSeqId(),
							beSectionMethod.getSubmitCode());
					break;
				}else {
					for(BeSectionExpertProgress beSectionExpertProgress:stepExpertProgressList) {
						//如果环节进度里的专家不存在与标段设置的专家里面，则重新同步专家进度表
						if(!expertList.contains(beSectionExpertProgress.getExpertId())) {
							beSectionExpertProgressService.addProgressList(beSectionInfo.getSeqId(),
									beSectionMethod.getSubmitCode());
							break label;
						}
					}
				}
			}
			
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	public void copyDataToSection(BeSectionMethod beSectionMethod) throws Exception{
		// 环节数据
		List<BeMethodStep> beMethodStepList = beMethodStepService.selectListByMethodId(beSectionMethod.getMethodId());
		beSectionStepService.addList(beSectionMethod, beMethodStepList);
	}
}
